/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rh.gui.impl;

import com.rh.game.Character;
import com.rh.game.EquipmentChange;
import com.rh.game.GameClothing;
import com.rh.game.GameObject;
import com.rh.game.HasEquipment;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Hayden
 */
public class CharacterJPanel extends JPanel {
    private static void debug(Object o)  {
        // System.out.println("CharacterJPanel: " + o);
    }
    
    private Character character = null;
    Character getCharacter()  { return character; }
    private void setCharacter(Character pWhom)  {
        character = pWhom;
    }

    private final JList listInventory = new JList();
    private final JList listWearing = new JList();

    private void putOn(final int index)  {
        debug("Inventory, Double clicked on Item " + index);
        Object o = listInventory.getModel().getElementAt(index);
        if (o instanceof GameClothing)  {
            GameClothing gc = (GameClothing) o;
            DefaultListModel dlm = (DefaultListModel) listWearing.getModel();
            dlm.addElement(gc);
            dlm = (DefaultListModel) listInventory.getModel();
            dlm.removeElement(gc);
        }
    }
    
    private void disrobe(final int index)  {
        debug("Wearing, Double clicked on Item " + index);                
        Object o = listWearing.getModel().getElementAt(index);
        if (o instanceof GameClothing)  {
            GameClothing gc = (GameClothing) o;
            DefaultListModel dlm = (DefaultListModel) listWearing.getModel();
            dlm.removeElement(gc);
            dlm = (DefaultListModel) listInventory.getModel();
            dlm.addElement(gc);
        }
    }
    
    public CharacterJPanel(Character pWhom)  {
        super();
        setCharacter(pWhom);
        initComponents();
    }

    private static GridBagConstraints gbc(int x, int y, int anchor)  {
        GridBagConstraints g = new GridBagConstraints();
        g.gridx = x;
        g.gridy = y;
        g.anchor = anchor;
        return g;
    }
    
    private void initComponents()  {
        setLayout(new BorderLayout());
        
        // The Left hand panel will have the hard stuff
        JPanel pnlLeft = new JPanel();
        GridBagLayout gbl = new GridBagLayout();
        pnlLeft.setLayout(gbl);
        JLabel lblGender = new JLabel("Gender:");
        pnlLeft.add(lblGender, gbc(0, 0, GridBagConstraints.EAST));
        
        JLabel lblGenderValue = new JLabel(getCharacter().getGender().toString());
        pnlLeft.add(lblGenderValue, gbc(1, 0, GridBagConstraints.WEST));
        
        // Wearing
        JLabel lblWearing = new JLabel("Wearing:");
        pnlLeft.add(lblWearing, gbc(0, 1, GridBagConstraints.EAST));

        DefaultListModel modelWearing = new DefaultListModel();
        for (GameClothing gc : getCharacter().getWearing())  {
            modelWearing.addElement(gc);
        }
        listWearing.setModel(modelWearing);
        listWearing.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        MouseListener mouseListener = new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {
                 if (e.getClickCount() == 2) {
                     disrobe(listWearing.locationToIndex(e.getPoint()));
                  }
             }
        };
        listWearing.addMouseListener(mouseListener);
        pnlLeft.add(listWearing, gbc(1, 1, GridBagConstraints.WEST));
        
        // Inventory
        JLabel lblInventory = new JLabel("Carrying:");
        pnlLeft.add(lblInventory, gbc(0, 2, GridBagConstraints.EAST));

        DefaultListModel modelInventory = new DefaultListModel();
        for (GameObject gc : getCharacter().getInventory())  {
            modelInventory.addElement(gc);
        }
        listInventory.setModel(modelInventory);
        mouseListener = new MouseAdapter() {
             public void mouseClicked(MouseEvent e) {
                 if (e.getClickCount() == 2) {
                     putOn(listInventory.locationToIndex(e.getPoint()));
                  }
             }
        };
        listInventory.addMouseListener(mouseListener);        
        listInventory.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        pnlLeft.add(listInventory, gbc(1, 2, GridBagConstraints.WEST));
        
        this.add(pnlLeft, BorderLayout.CENTER);
        
        // The Right hand panel will handle an image of the character
        ImageJPanel imageHolder = new ImageJPanel(getCharacter().getImage());
        this.add(imageHolder, BorderLayout.EAST);
    }
    
    public static EquipmentChange character(Character whom)  {
        CharacterJPanel cjp = new CharacterJPanel(whom);
        int option = JOptionPane.showConfirmDialog(null, cjp, "Character: " + whom.getName(), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (option == JOptionPane.CANCEL_OPTION)  return EquipmentChange.NULL;
        
        HasEquipment he = new HasEquipment1();
        for (int i1=0; i1<cjp.listWearing.getModel().getSize(); i1++)  {
            he.getWearing().add((GameClothing) cjp.listWearing.getModel().getElementAt(i1));
        }
        for (int i1=0; i1<cjp.listInventory.getModel().getSize(); i1++)  {
            he.getInventory().add((GameObject) cjp.listInventory.getModel().getElementAt(i1));
        }
        return Gui1.computeEquipmentChange(whom, he);
   }    
}
